/*
 * GuudMap.cpp
 *
 *  Created on: 2013-1-28
 *      Author: heqiu
 */

#include "GuudMap.h"

namespace guud {
GuudMap::GuudMap(uint32_t uiShortUnit, uint32_t uiLongUnit,
		uint32_t uiUnitPixel) :
		GuudCanvas(uiShortUnit, uiLongUnit, uiUnitPixel) {
	m_pBuilding = new GuudBuildingPtr[(uiLongUnit + 1) * (uiLongUnit + 1)];
	m_bitSetWay.init((uiLongUnit + 1) * (uiLongUnit + 1));
	for (uint32_t ui = 0; ui < (uiLongUnit + 1) * (uiLongUnit + 1); ui++) {
		m_pBuilding[ui] = NULL;
	}
}
GuudMap::~GuudMap() {
	if (m_pBuilding != NULL) {
		delete[] m_pBuilding;
		m_pBuilding = NULL;
	}
}

GuudBuildingPtr GuudMap::getBuilding(uint32_t uiXUnit, uint32_t uiYUnit) {
	if (uiXUnit > m_uiLongUnit || uiYUnit > m_uiLongUnit) {
		return NULL;
	}
	uint32_t uiPos = uiXUnit * (m_uiLongUnit + 1) + uiYUnit;
	return m_pBuilding[uiPos];
}
bool GuudMap::testBuilding(GuudBuilding & stNode) {
	for (uint32_t ui = 0; ui < (uint32_t) stNode.m_iXLenUnit; ui++) {
		for (uint32_t uj = 0; uj < (uint32_t) stNode.m_iYLenUnit; uj++) {
			GuudPoint stPoint(stNode.m_gpPosUnit.m_iX - ui,
					stNode.m_gpPosUnit.m_iY - uj);
			if (!bUnitIn(stPoint)) {
				return false;
			}
			//已经有建筑
			if (getBuilding(stPoint.m_iX, stPoint.m_iY) > 0) {
				return false;
			}
		}
	}
	return true;
}
bool GuudMap::setBuilding(GuudBuilding & stNode) {
	if (testBuilding(stNode)) {
		for (uint32_t ui = 0; ui < (uint32_t) stNode.m_iXLenUnit; ui++) {
			for (uint32_t uj = 0; uj < (uint32_t) stNode.m_iYLenUnit; uj++) {
				uint32_t uiX = stNode.m_gpPosUnit.m_iX - ui;
				uint32_t uiY = stNode.m_gpPosUnit.m_iY - uj;
				uint32_t uiPos = uiX * (m_uiLongUnit + 1) + uiY;
				m_pBuilding[uiPos] = &stNode;
			}
		}
		return true;
	}
	return false;
}
}

